package Aleehot100;

/**
 * ClassName: c042
 * Package: Aleehot100
 * Description: 108. 将有序数组转换为二叉搜索树
 *
 * @Author BCXJ
 * @Create 2025/7/11 08:40
 * @Version 1.0
 * @Since 1.0
 */
public class c042 {
    public static void main(String[] args) {

    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * 递归方法解决
     * @param nums
     * @return
     */
    public TreeNode sortedArrayToBST(int[] nums) {
       return process(nums, 0, nums.length - 1);
    }

    private TreeNode process(int[] nums, int l, int r) {
        int mid = (l + r) << 1;
        TreeNode root = new TreeNode(nums[mid]);
        if(l == r) {
            return root;
        }

         root.left = process(nums, l, mid);
         root.right = process(nums, mid + 1, r);
         return root;
    }


}
